package com.springboot.erp.service.impl;


import com.springboot.erp.entity.Order;
import com.springboot.erp.mapper.OrderMapper;
import com.springboot.erp.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
/**
 * 订单服务实现类
 */
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 创建订单
     */
    @Override
    public boolean createOrder(Order order) {
        // 插入订单
        int orderResult = orderMapper.insertOrder(order);
        if (orderResult > 0) {
            // 插入账户项目
            int accountResult = orderMapper.insertDepotItem(order);
            return accountResult > 0;
        }
        return false;
    }

    /**
     * 查询订单
     */
    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectOrderList(id);
    }

    /**
     * 修改订单
     */
    @Override
    public boolean updateOrder(Order order) {
        // 更新订单
        int orderResult = orderMapper.updateOrderById(order);
        if (orderResult > 0) {
            // 更新账户项目
            int accountResult = orderMapper.updateOrderItem(order);
            return accountResult > 0;
        }
        return false;
    }

    /**
     * 删除订单
     */
    @Override
    public boolean deleteOrder(Long id) {
        // 先删除子表数据
        int itemResult = orderMapper.deleteDepotItemByHeaderId(id);
        // 再删除主表数据
        int orderResult = orderMapper.deleteOrderById(id);
        return orderResult > 0;
    }
}